﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Yule8xCommon.Applib;
using Yule8xCommon.Typelib;
using Yule8xModel;
using Yule8xWechat.Applib;

namespace Yule8xWechat.Promotion
{
    public partial class GetFreeDiamond : System.Web.UI.Page
    {
        protected WxUserInfoResult wxUser = null;
        protected int user_id;
        protected bool isDailyDrawedCard; // 是否已经免费领取过了
        protected int dailyGetDimondNumber; // 每日领卡数量
        protected int refererEachDimondNumber; // 推广普通用户获得钻石数量
        protected int refererDailyMaxDimondNumber; // // 推广玩家每日最大赠卡数量
        protected void Page_Load(object sender, EventArgs e)
        {
            AppInfo.IsDebugMode = RuntimeDataUtil.CheckDebugModel();

            user_id = RequestUtil.GetInteger("user_id");
            if (AppInfo.IsDebugMode && user_id > 0)
            {
                wxUser = UserModels.GetWxUserInfo(user_id);
                if (null == wxUser)
                {
                    ResponseUtil.WriteEnd("user_id无效!");
                }
            }
            else
            {
                wxUser = LoginPageBase.GetLoginInfo();
                if (null == wxUser)
                {
                    Response.Redirect(WechatUrls.GetUserInfoUrl("promotion"), true);
                }
            }
            if (UserModels.CheckUserDailyDrawDimondStatus(wxUser.user_id))
            {
                RedirectUtil.RedirectTimer(false, "重复领取", new string[] { "用户只能领取一次钻石，请勿重复领取!" }, "../Agent/UserIndex.aspx?user_id=" + user_id);
            }
            // 强制要有gameid
            if (wxUser.game_id_bozhong < 1)
            {
                // 获取并同步gameid
                string error;
                wxUser.game_id_bozhong = UserModels.GetGameID(wxUser.user_id, null, "bozhong", out error);
                if (wxUser.game_id_bozhong > 0)
                {
                    Session["login"] = wxUser; // 重新保存带有gameid的会话消息
                }
            }
            if (wxUser.game_id_wanghu < 1)
            {
                // 获取并同步gameid
                string error;
                wxUser.game_id_wanghu = UserModels.GetGameID(wxUser.user_id, null, "wanghu", out error);
                if (wxUser.game_id_bozhong > 0)
                {
                    Session["login"] = wxUser; // 重新保存带有gameid的会话消息
                }
            }
            // 没有任何游戏ID则提示下载游戏
            if (wxUser.game_id_wanghu < 1 && wxUser.game_id_bozhong < 1)
            {
                Response.Redirect("NoGameID.aspx", true);
            }
            LogUtil.Instance.WriteInfo("[我的推广/每日免费领卡] 已登录");
            int uid = wxUser.user_id;

            dailyGetDimondNumber = AppConf.DailyCardCount; // 每日领卡数量

            // 推广玩家每日最大赠卡数量
            refererEachDimondNumber = AppConf.ReferralUserEachCardCount; // 推广普通用户获得钻石数量
            int refereralMaxDimondNumber = AppConf.ReferralUserDailyMaxCardCount; // 推广玩家每日最大赠卡数量
            refererDailyMaxDimondNumber = UserModels.GetReferralUserDailyMaxNumber(uid, DateTime.Today, refereralMaxDimondNumber);
            // 判断每日领卡状态
            isDailyDrawedCard = UserModels.CheckUserDailyDrawDimondStatus(uid);
            string action = RequestUtil.FormString("action");
            if ("submit" == action)
            {
                if (isDailyDrawedCard)
                {
                    RedirectUtil.RedirectTimer(false, "重复操作", new string[] { "您已经领过钻石了，请勿重复领取!" }, "../Agent/UserIndex.aspx?user_id=" + user_id);
                }
                else
                {
                    int game_mact_id = RequestUtil.FormInteger("game_mact_id");
                    int game_id = RequestUtil.FormInteger("game_id");
                    if (game_mact_id < 1 || game_id < 1)
                    {
                        RedirectUtil.RedirectTimer(false, "游戏数据有误", new string[] { "未选择游戏或游戏ID无效!" });
                    }
                    string outputMsg;
                    int game_currency_number = dailyGetDimondNumber;
                    switch (game_mact_id)
                    {
                        case 1:
                            game_currency_number = dailyGetDimondNumber * AppConf.GameCurrencyRate_Bozhong;
                            break;
                        case 2:
                            game_currency_number = dailyGetDimondNumber * AppConf.GameCurrencyRate_Wanghu;
                            break;
                    }
                    bool result = UserModels.UserDailyGetDimond(uid, DateTime.Today, dailyGetDimondNumber, game_currency_number, game_id, game_mact_id, out outputMsg);

                    LogUtil.Instance.WriteInfo(string.Format("[我的推广/每日免费领卡] UserModels.UserDailyGetDimond({0}, DateTime.Today, {1}, {2}, 0, out outputMsg); {3}: {4}", uid, dailyGetDimondNumber, wxUser.game_id_bozhong, result, outputMsg));

                    RedirectUtil.RedirectTimer(true, "领取成功", new string[] { "领取成功, 您可以体验游戏了!" }, "../Agent/UserIndex.aspx?user_id=" + user_id);
                }
            }
        }
    }
}